package goroutine

import (
	"fmt"
	"strconv"
	"time"
)

func Sum(channel chan int) {
	sumNumber := 0

	for {
		number, okay := <-channel
		fmt.Println("收到数据", number, okay)
		if okay {
			sumNumber += number
		} else {
			fmt.Println("求得总合为" + strconv.Itoa(sumNumber))
			return
		}
	}
}

func DemoWithBuffChannel() {
	list := [101]int{}
	channel := make(chan int, 1000)
	go Sum(channel)

	for index, _ := range list {
		channel <- index
	}
	close(channel)
	// 当 channel 关闭的时候 number, okay := <-channel 结果是
	// okay == false
	time.Sleep(time.Nanosecond)
}
